package com.wangxiang.shop.member.service.Impl;

import com.wangxiang.shop.member.dao.ReceiptDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangxiang.common.utils.PageUtils;
import com.wangxiang.common.utils.Query;


import com.wangxiang.shop.member.entity.ReceiptEntity;
import com.wangxiang.shop.member.service.ReceiptService;


@Service("receiptService")
public class ReceiptServiceImpl extends ServiceImpl<ReceiptDao, ReceiptEntity> implements ReceiptService {
    @Autowired
    private ReceiptDao receiptDao;

    @Override
    public int add(HashMap<String, String> map) {
        int userId = Integer.valueOf(map.get("userId"));
        List<ReceiptEntity> list = receiptDao.getListByUserId(userId);
        ReceiptEntity receiptEntity = new ReceiptEntity();
        receiptEntity.setUserId(userId);
        receiptEntity.setName(map.get("name"));
        receiptEntity.setMobail(map.get("mobail"));
        receiptEntity.setProvince(map.get("province"));
        receiptEntity.setCity(map.get("city"));
        receiptEntity.setArea(map.get("area"));
        receiptEntity.setLocaltion(map.get("localtion"));
        if (list.size()==0){
            receiptEntity.setStatus(0); //0 默认 1 一般 第一条记录为默认
        }else {
            receiptEntity.setStatus(1); //0 默认 1 一般
        }

        int insert = receiptDao.insert(receiptEntity);
        if (insert>0){
            return insert;
        }
        return 0;
    }

    @Override
    public List<ReceiptEntity> list(int userId) {

        List<ReceiptEntity> listByUserId = receiptDao.getListByUserId(userId);
        ArrayList<ReceiptEntity> receiptEntities = new ArrayList<>();
        for (ReceiptEntity receiptEntity : listByUserId) {
            if (receiptEntity.getStatus()!=2){
                receiptEntities.add(receiptEntity);
            }
        }
        return receiptEntities;
    }

    @Override
    public int update(HashMap<String, String> map) {
        int id = Integer.valueOf(map.get("receiptId"));
        ReceiptEntity receiptEntity = receiptDao.selectById(id);
        int status = Integer.valueOf(map.get("status"));
//        System.out.println("status"+status);
//        System.out.println("db=="+receiptEntity);
//        System.out.println(status==receiptEntity.getStatus());

        //相同,不修改id,userid,status
        if (status==receiptEntity.getStatus()){
            System.out.println("分支一");
            ReceiptEntity entity = new ReceiptEntity();
            entity.setId(receiptEntity.getId());
            entity.setUserId(receiptEntity.getUserId());
            entity.setStatus(receiptEntity.getStatus());
            if (map.get("name")!=null){
                entity.setName(map.get("name"));
            }
            if (map.get("mobail")!=null){
                entity.setMobail(map.get("mobail"));
            }
            if (map.get("province")!=null){
                entity.setProvince(map.get("province"));
            }
            if (map.get("city")!=null){
                entity.setCity(map.get("city"));
            }
            if (map.get("area")!=null){
                entity.setArea(map.get("area"));
            }
            if (map.get("localtion")!=null){
                entity.setLocaltion(map.get("localtion"));
            }
            int i = receiptDao.updateById(entity);
            return i;
        }
        //不相等
        if (receiptEntity.getStatus()!=status){
            System.out.println("不相等流程");
            //数据库默认，前端取消默认,该用户下没有默认地址
            if (receiptEntity.getStatus()==0&status==1){
                System.out.println("流程1");
                ReceiptEntity entity = new ReceiptEntity();
                entity.setId(receiptEntity.getId());
                entity.setUserId(receiptEntity.getUserId());
                entity.setStatus(status);
                if (map.get("name")!=null){
                    entity.setName(map.get("name"));
                }
                if (map.get("mobail")!=null){
                    entity.setMobail(map.get("mobail"));
                }
                if (map.get("province")!=null){
                    entity.setProvince(map.get("province"));
                }
                if (map.get("city")!=null){
                    entity.setCity(map.get("city"));
                }
                if (map.get("area")!=null){
                    entity.setArea(map.get("area"));
                }
                if (map.get("localtion")!=null){
                    entity.setLocaltion(map.get("localtion"));
                }
                int i = receiptDao.updateById(entity);
                return i;
            }
            //数据库否，前端默认，更替默认
            if (receiptEntity.getStatus()==1&status==0){
                System.out.println("流程2");
                ArrayList<ReceiptEntity> receiptEntities = new ArrayList<>();
                List<ReceiptEntity> list = receiptDao.getListByUserId(receiptEntity.getUserId());
                list.forEach((item)->{
                   if (item.getStatus()==0){
                       receiptEntities.add(item);
                   }
               });
                receiptEntities.forEach((item)->{
                    receiptDao.updateStatus(item.getId(),1);
                });
                ReceiptEntity entity = new ReceiptEntity();
                entity.setId(receiptEntity.getId());
                entity.setUserId(receiptEntity.getUserId());
                entity.setStatus(status);
                if (map.get("name")!=null){
                    entity.setName(map.get("name"));
                }
                if (map.get("mobail")!=null){
                    entity.setMobail(map.get("mobail"));
                }
                if (map.get("province")!=null){
                    entity.setProvince(map.get("province"));
                }
                if (map.get("city")!=null){
                    entity.setCity(map.get("city"));
                }
                if (map.get("area")!=null){
                    entity.setArea(map.get("area"));
                }
                if (map.get("localtion")!=null){
                    entity.setLocaltion(map.get("localtion"));
                }
                int i = receiptDao.updateById(entity);
                return i;

            }
        }


        return 0;
        }

    @Override
    public int delete(int id) {
        int i = receiptDao.updateStatus(id, 2);
        return i;
    }


}